﻿Imports ZLDNN.Modules.DNNArticle
Imports System.Globalization
Imports Aricie.Services

Namespace Aricie.DNN.Services.Url.FriendlyUrl.Providers

    Public Class DnnArticleRewriter
        Implements IUrlRewriterProvider
        Implements IIsActive

        Public Function GetRewrites() As System.Collections.Generic.List(Of GroupParamsRewrite) Implements IUrlRewriterProvider.GetRewrites

            Dim toReturn As New List(Of GroupParamsRewrite)

            Dim desc As String = "Title of the current Article, filtered according to the general parameters"
            toReturn.Add(New GroupParamsRewrite("ArticleTitle", desc, RewriteType.SubPath, UrlParam.FromSyntax("ArticleId")))

            desc = "Name of the current Ventrian NewsArticle Article's category, filtered according to the general parameters"
            toReturn.Add(New GroupParamsRewrite("CategoryTitle", desc, RewriteType.SubPath, UrlParam.FromSyntax("cid")))

            Return toReturn
        End Function

        Public Function RewriteParams(ByVal groupName As String, ByVal objRewriteType As RewriteType, ByVal params As System.Collections.Generic.Dictionary(Of UrlParam, String), ByVal behaviour As FriendlierUrlStrategy) As GroupRewriteResult Implements IUrlRewriterProvider.RewriteParams

            Dim toReturn As New GroupRewriteResult()

            If params.Count > 0 Then
                Select Case groupName.ToLowerInvariant
                    Case "articletitle"
                        Dim _
                            articleId As Integer = _
                                Integer.Parse(params(UrlParam.FromSyntax("articleid")), CultureInfo.InvariantCulture)
                        toReturn.RewriteValue = GetArticleTitle(articleId, behaviour)
                        toReturn.ConsumedParameters(UrlParam.FromSyntax("articleid")) = True
                    Case "categorytitle"
                        Dim _
                            categoryid As Integer = _
                                Integer.Parse(params(UrlParam.FromSyntax("cid")), CultureInfo.InvariantCulture)
                        toReturn.RewriteValue = GetCategoryTitle(categoryid, behaviour)
                        toReturn.ConsumedParameters(UrlParam.FromSyntax("cid")) = True
                End Select
            End If
            Return toReturn

        End Function

        Private Function GetArticleTitle(ByVal articleId As Integer, ByVal behaviour As FriendlierUrlStrategy) As String


            Dim articles As Dictionary(Of Integer, DNNArticleInfo) = CacheHelper.GetCache("FUP-DnnArticle-Articles")

            If articles Is Nothing Then

                articles = New Dictionary(Of Integer, DNNArticleInfo)

                Dim ac As New DNNArticleController()
                Dim condition As New SearchCondition()

                For Each a As DNNArticleInfo In ac.GetTopArticlesBySQL(condition, "", 0, -1, False)
                    articles.Add(a.ItemId, a)
                Next

                CacheHelper.SetCacheDependant("FUP-DnnArticle-Articles", articles, behaviour.AbsoluteCacheDuration, FriendlierUrlProvider.glbRewritesCacheKey)
            End If

            Dim targetArticle As DNNArticleInfo = Nothing
            If Not articles.TryGetValue(articleId, targetArticle) Then
                Throw New ApplicationException(String.Format("Article with articleId {0} not found for url rewriting", articleId.ToString(CultureInfo.InvariantCulture)))
            End If
            Return targetArticle.Title
        End Function


        Private Function GetCategoryTitle(ByVal cid As Integer, ByVal behaviour As FriendlierUrlStrategy) As String

            Dim categories As Dictionary(Of Integer, CategoryInfo) = CacheHelper.GetCache("FUP-DnnArticle-Categories")

            If categories Is Nothing Then

                categories = New Dictionary(Of Integer, CategoryInfo)

                Dim cc As New CategoryController

                For Each c As CategoryInfo In cc.List()
                    categories.Add(c.ItemID, c)
                Next

                CacheHelper.SetCacheDependant("FUP-DnnArticle-Categories", categories, behaviour.AbsoluteCacheDuration, FriendlierUrlProvider.glbRewritesCacheKey)
            End If

            Dim targetCat As CategoryInfo = Nothing
            If Not categories.TryGetValue(cid, targetCat) Then
                Throw New ApplicationException(String.Format("Category with categoryid {0} not found for url rewriting", cid.ToString(CultureInfo.InvariantCulture)))
            End If
            Return targetCat.Title

        End Function

        Public ReadOnly Property IsActive As Boolean Implements IIsActive.IsActive
            Get
                'ZLDNN.Modules.DNNArticle.DNNArticleController
                Return DotNetNuke.Framework.Reflection.CreateType("ZLDNN.Modules.DNNArticle.DNNArticleController", True) IsNot Nothing
            End Get
        End Property
    End Class

End Namespace
